// 用户认证服务 - 使用本地存储
class AuthService {
  constructor() {
    this.USERS_KEY = 'pet_rescue_users'
    this.CURRENT_USER_KEY = 'pet_rescue_current_user'
  }

  // 获取所有用户
  getUsers() {
    const users = localStorage.getItem(this.USERS_KEY)
    return users ? JSON.parse(users) : []
  }

  // 保存用户列表
  saveUsers(users) {
    localStorage.setItem(this.USERS_KEY, JSON.stringify(users))
  }

  // 获取当前登录用户
  getCurrentUser() {
    const user = localStorage.getItem(this.CURRENT_USER_KEY)
    return user ? JSON.parse(user) : null
  }

  // 保存当前登录用户
  setCurrentUser(user) {
    localStorage.setItem(this.CURRENT_USER_KEY, JSON.stringify(user))
  }

  // 清除当前登录用户
  clearCurrentUser() {
    localStorage.removeItem(this.CURRENT_USER_KEY)
  }

  // 检查用户是否已注册
  isUserExists(username) {
    const users = this.getUsers()
    return users.some(user => user.username === username)
  }

  // 用户注册
  register(userData) {
    const { username, password, email, nickname } = userData
    
    // 检查用户名是否已存在
    if (this.isUserExists(username)) {
      return {
        success: false,
        message: '用户名已存在'
      }
    }

    // 简单验证
    if (!username || username.length < 3) {
      return {
        success: false,
        message: '用户名至少需要3个字符'
      }
    }

    if (!password || password.length < 6) {
      return {
        success: false,
        message: '密码至少需要6个字符'
      }
    }

    // 创建新用户
    const newUser = {
      id: Date.now().toString(),
      username,
      password, // 实际项目中应该加密密码
      email: email || '',
      nickname: nickname || username,
      registerTime: new Date().toISOString(),
      avatar: ''
    }

    // 保存用户
    const users = this.getUsers()
    users.push(newUser)
    this.saveUsers(users)

    return {
      success: true,
      message: '注册成功',
      user: { ...newUser, password: undefined } // 不返回密码
    }
  }

  // 用户登录
  login(username, password) {
    const users = this.getUsers()
    const user = users.find(u => u.username === username)

    if (!user) {
      return {
        success: false,
        message: '用户不存在'
      }
    }

    if (user.password !== password) {
      return {
        success: false,
        message: '密码错误'
      }
    }

    // 登录成功，保存当前用户
    const userInfo = { ...user, password: undefined }
    this.setCurrentUser(userInfo)

    return {
      success: true,
      message: '登录成功',
      user: userInfo
    }
  }

  // 用户退出登录
  logout() {
    this.clearCurrentUser()
    return {
      success: true,
      message: '已退出登录'
    }
  }

  // 检查是否已登录
  isLoggedIn() {
    return this.getCurrentUser() !== null
  }

  // 更新用户信息
  updateUser(userData) {
    const currentUser = this.getCurrentUser()
    if (!currentUser) {
      return {
        success: false,
        message: '请先登录'
      }
    }

    const users = this.getUsers()
    const userIndex = users.findIndex(u => u.id === currentUser.id)
    
    if (userIndex === -1) {
      return {
        success: false,
        message: '用户不存在'
      }
    }

    // 更新用户信息
    const updatedUser = { ...users[userIndex], ...userData, id: currentUser.id }
    users[userIndex] = updatedUser
    this.saveUsers(users)

    // 更新当前登录用户信息
    const userInfo = { ...updatedUser, password: undefined }
    this.setCurrentUser(userInfo)

    return {
      success: true,
      message: '用户信息更新成功',
      user: userInfo
    }
  }
}

export default new AuthService()
